home *** CD-ROM | disk | FTP | other *** search
- /*
- * Compile with:
- * cc string.o array.o dict.o cgilib.o \
- * ht72a.c -o ht72a.cgi -lgd -lm
- */
-
- #include <stdio.h>
-
- #include <gd.h>
- #include <gdfontmb.h> /* support for the Medium-Bold font */
- #include <gdfonts.h> /* support for the small font */
-
- #include "cgilib.h"
-
-
- void centerString(
- gdImagePtr img, /* gd Image to render in */
- gdFontPtr font, /* gd Font to render with */
- int x, int y, /* x and y coordinates of center of string*/
- char *txt, /* text to render */
- int colour) /* colour to render the text in */
- {
- /* Calculate the width of the text in pixels */
- int width = strlen(txt)*(font->w);
-
- gdImageString(img,font,1+x-width/2,y,txt,colour);
- }
-
-
- int main(void)
- {
- Dictionary dataDict = readParse();
- int max, i, x, barLen;
- char buf[80];
-
- int barWidth = dict_isKey(dataDict,"barWidth")
- ? atoi(dict_valueForKey(dataDict,"barWidth"))
- : 20;
-
- int barHeight = dict_isKey(dataDict,"barHeight")
- ? atoi(dict_valueForKey(dataDict,"barHeight"))
- : 100;
-
- int barPadding = dict_isKey(dataDict,"barPadding")
- ? atoi(dict_valueForKey(dataDict,"barPadding"))
- : 2;
-
- int score[10] = {1,2,3,4,5,6,7,8,9,10};
-
- int topPadding = 15;
- int bottomPadding = 20;
- int xAxis = topPadding+barHeight;
- int width = 10*barWidth+11*barPadding;
- int height = barHeight+topPadding+bottomPadding;
-
- /* Create a blank gd image of the required size */
- gdImagePtr im = gdImageCreate(width, height);
-
- /* Allocate colors */
- int black = gdImageColorAllocate(im, 0, 0, 0);
- int white = gdImageColorAllocate(im,255,255,255);
- int blue = gdImageColorAllocate(im, 0, 0,255);
-
- /*
- * Overide the defaults for score[] if the CGI
- * variable 'score' was specified
- */
- if (dict_isKey(dataDict,"score"))
- sscanf(dict_valueForKey(dataDict,"score"),
- "%d %d %d %d %d %d %d %d %d %d",
- &score[0], &score[1], &score[2], &score[3],
- &score[4], &score[5], &score[6], &score[7],
- &score[8], &score[9]);
-
- /* Make the image interlaced */
- gdImageInterlace(im, 1);
-
- /* Output the GIF content type */
- printf("Content-type: image/gif\n\n");
-
- /* Draw the X-axis */
- gdImageLine(im, 0, xAxis, width-1, xAxis, white);
-
- /* Determine the height of the highest bar */
- for ( max=0, i=0 ; i<10 ; i++ )
- {
- if (score[i] > max)
- {
- max = score[i];
- }
-
- }
-
- for ( i=0 ; i<10 ; i++ )
- {
- /* Compute the X coordinate for the center of the bar */
- x = i*barWidth+barWidth/2+(i+1)*barPadding;
-
- /* Calculate how high this bar should be */
- barLen = score[i]*barHeight/max;
-
- /* Draw a tick mark on the x-axis */
- gdImageLine(im, x, xAxis+1, x, xAxis+2, white);
-
- /*
- * Label the x-axis with a number from 1 to 10
- * using the bold font.
- */
- sprintf(buf,"%d",i+1);
- centerString(im,
- gdFontMediumBold,
- x,xAxis+3, /* X, Y coordinates */
- buf, /* text to display */
- white);
-
- /* label the height of the bar using the small font */
- sprintf(buf,"%d",score[i]);
- centerString(im,
- gdFontSmall,
- x,xAxis-barLen-11,
- buf,
- white);
-
- /* draw one bar */
- gdImageFilledRectangle(
- im,
- x-barWidth/2, xAxis-barLen,
- x+barWidth/2, xAxis-1,
- blue);
- }
-
- /*
- * Convert the image to GIF and print it to
- * standard output
- */
- gdImageGif(im, stdout);
-
- /* Destroy image */
- gdImageDestroy(im);
-
- return 0;
- }
-